/**
 * Title: Hyperbolic engine
 * Description: jFamilyTree Engine
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

import jFamilyTree.control.Controller;

/**
 * Tool bar
 * 
 * @author Bernard Bou
 */
public class Toolbar extends JToolBar
{
	// D A T A

	static private final long serialVersionUID = 1L;

	/**
	 * Controller to send action requests to
	 */
	private final Controller theController;

	/**
	 * Vertical layout
	 */
	static public final boolean isVertical = true;

	// C O N S T R U C T O R
	
	/**
	 * Constructor
	 */
	public Toolbar(Controller thisController, boolean hasTooltip, boolean tooltipDisplaysContent, boolean hyperbolicEdges, boolean focusOnHover)
	{
		super(isVertical ? VERTICAL : HORIZONTAL);
		theController = thisController;
		setFloatable(true);

		add(makeButton("reset.png", "Reset", Controller.Command.HOME));
		add(makeButton("refresh.png", "Refresh", Controller.Command.REFRESH));
		addSeparator();
		add(makeButton("north.png", "North", Controller.Command.NORTH));
		add(makeButton("south.png", "South", Controller.Command.SOUTH));
		add(makeButton("east.png", "East", Controller.Command.EAST));
		add(makeButton("west.png", "West", Controller.Command.WEST));
		add(makeButton("radial.png", "Radial", Controller.Command.RADIAL));
		addSeparator();
		add(makeButton("expand.png", "Expand", Controller.Command.EXPAND));
		add(makeButton("shrink.png", "Shrink", Controller.Command.SHRINK));
		add(makeButton("widen.png", "Widen", Controller.Command.WIDEN));
		add(makeButton("narrow.png", "Narrow", Controller.Command.NARROW));
		addSeparator();
		add(makeToggle("arc.png", "noarc.png", "Hyperbolic/Line", hyperbolicEdges, Controller.Command.HYPERBOLICEDGE));
		addSeparator();
		add(makeButton("zoomin.png", "Zoom In", Controller.Command.ZOOMIN));
		add(makeButton("zoomout.png", "Zoom Out", Controller.Command.ZOOMOUT));
		addSeparator();
		add(makeToggle("nodetipyes.png", "nodetipno.png", "Tooltip toggle", hasTooltip, Controller.Command.TOOLTIP));
		add(makeToggle("nodetipbig.png", "nodetipsmall.png", "Tooltip displays content", tooltipDisplaysContent, Controller.Command.TOOLTIPCONTENT));
		add(makeToggle("hoverfocus.png", "nohoverfocus.png", "Hovering triggers focus", focusOnHover, Controller.Command.FOCUSHOVER));
	}

	/**
	 * Make button
	 * 
	 * @param thisIconString
	 *        icon
	 * @param thisToolTip
	 *        tooltip
	 * @param thisCommand
	 *        index
	 * @return button
	 */
	private JButton makeButton(String thisIconString, String thisToolTip, final Controller.Command thisCommand)
	{
		JButton thisButton = new JButton();
		thisButton.setBorder(null);
		thisButton.setToolTipText(thisToolTip);
		thisButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(thisCommand);
			}
		});
		thisButton.setIcon(new ImageIcon(Toolbar.class.getResource("images/" + thisIconString)));
		return thisButton;
	}

	/**
	 * Make toggle button
	 * 
	 * @param thisIconString
	 *        icon
	 * @param thisSelectedIcon
	 *        pressed icon
	 * @param thisToolTip
	 *        tooltip
	 * @param thisState
	 *        tooltip
	 * @param thisCommand
	 *        index
	 * @return button
	 */
	private JToggleButton makeToggle(String thisIconString, String thisSelectedIcon, String thisToolTip, boolean thisState, final Controller.Command thisCommand)
	{
		JToggleButton thisButton = new JToggleButton();
		thisButton.setBorder(null);
		thisButton.setToolTipText(thisToolTip);
		thisButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(thisCommand);
			}
		});
		thisButton.setIcon(new ImageIcon(Toolbar.class.getResource("images/" + thisIconString)));
		thisButton.setSelectedIcon(new ImageIcon(Toolbar.class.getResource("images/" + thisSelectedIcon)));
		return thisButton;
	}
}
